<?php

class Tarifa {

    function GetFee($OtherFee, $Origin, $Fare) {
        $OriType = $GLOBALS['MYT']->db_array("SELECT OriTipo FROM origenes
			 WHERE OriNumero = $Origin");
        if ($OriType[1]['OriTipo'] == 1 || $OriType[1]['OriTipo'] == 4) {
            /* $RTax = $GLOBALS['MYT']->db_array("SELECT * FROM rd_taxes
              WHERE STATUS = 1
              AND QUOTAGE = {$_POST['RTQuot']}
              AND ORMODE = 'ORIGEN'
              AND VALUE = {$OriType[1]['OriTipo']}"); */
            $RTax = $GLOBALS['MYT']->db_array("SELECT * FROM rd_taxes
			 WHERE STATUS = 1
			 AND QUOTAGE = 0
			 AND ORMODE = 'ORIGEN'
			 AND VALUE = {$OriType[1]['OriTipo']}");
        } else {
            /* $RTax = $GLOBALS['MYT']->db_array("SELECT * FROM rd_taxes
              WHERE STATUS = 1
              AND QUOTAGE = {$_POST['RTQuot']}
              AND ORMODE = 'TYPE'
              AND VALUE = {$_POST['FraUrgente']}"); */
            $RTax = $GLOBALS['MYT']->db_array("SELECT * FROM rd_taxes
			 WHERE STATUS = 1
			 AND QUOTAGE = 0
			 AND ORMODE = 'TYPE'
			 AND VALUE = $Fare");
        }
        if ($RTax[1]['TYPE'] == 'FIXED') {
            $RTTax = number_format($RTax[1]['TAXVALUE'], 2);
        } elseif ($RTax[1]['TYPE'] == 'PERCENTAGE') {
            $RTTax = number_format($OtherFee * ($RTax[1]['TAXVALUE'] / 100), 2);
            if ($RTax[1]['TAXMAX'] > 0 && $RTTax > $RTax[1]['TAXMAX']) {
                $RTTax = number_format($RTax[1]['TAXMAX'], 2);
            }
        }
        return $RTTax;
    }

    function CalculaTarifa($CorrID, $CountryID, $Valor) {
        $Return = array();
        $TarifaValor = array(
            0 => array(
                100, 200, 350, 500, 850, 1200, 1600, 2000, 3000, 5000, 1000000000
            ),
            804 => array(
                100, 200, 300, 350, 500, 600, 850, 1000, 1200, 1600, 2000, 1000000000
            ),
            498 => array(
                100, 200, 350, 600, 850, 1000, 1200, 1500, 1600, 2000, 3000, 5000, 1000000000
            )
        );


        $TarifaMoney = array(
            0 => array(
                1.50, 2, 1, 2, 3, 8, 9, 20, 50, 60, 70
            ),
            804 => array(
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0
            ),
            498 => array(
                3, 3, 3, 4, 6, 12, 10, 12, 12, 10, 20, 50, 80
            )
        );

        $TarifaPrivate = array(
            0 => array(
                3, 5, 9, 12, 15, 15, 20, 24, 30, 30, 30
            ),
            804 => array(
                3, 5, 9, 9, 12, 12, 15, 15, 20, 20, 24, 30
            ),
            498 => array(
                3, 5, 9, 12, 15, 15, 20, 20, 24, 30, 30, 30, 30
            )
        );

        $ImpostoSelo = array(
            0 => array(
                4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
            ),
            804 => array(
                4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
            ),
            498 => array(
                4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
            )
        );

        if ($CorrID == 307) {

            if (!isset($TarifaValor[$CountryID])) {
                $CountryID = 0;
            }

            $count = count($TarifaValor[$CountryID]);
            $pos = 0;
            for ($i = 0; $i < $count; $i++) {
                if ($Valor <= $TarifaValor[$CountryID][$i]) {
                    $i = $count;
                } else {
                    $pos++;
                }
            }

            # $pos = array_search(, $TarifaValor[$CountryID]);

            $Return['TaxaMoney'] = $TarifaMoney[$CountryID][$pos];
            $Return['TaxaCorr'] = $TarifaPrivate[$CountryID][$pos];
            $TaxaTot = $Return['TaxaMoney'] + $Return['TaxaCorr'];
            $Return['ImpSelo'] = round($TaxaTot * $ImpostoSelo[$CountryID][$pos] / 100, 2);
            $Return['TaxaTotal'] = $Return['ImpSelo'] + $TaxaTot;
            if (!is_array($Return)) {
                die("Erro:Impossivel calcular taxa");
            } else {
                return $Return;
            }
        }
    }

}

?>
